package com.qys.livesMall.main.test;

/**
 * @packageName:
 * @className:DelayedMessageDemo
 * @Description:
 * @author:guomx
 * @date:2023/7/2911:34
 */

import org.redisson.Redisson;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RQueue;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

import java.util.concurrent.TimeUnit;

public class DelayedMessageDemo {
    public static void main(String[] args) throws InterruptedException {
        Config config = new Config();
        config.useSingleServer().setAddress("redis://localhost:6379");
        RedissonClient redissonClient = Redisson.create(config);

        // 获取延时任务队列
        RQueue<String> delayedQueue = redissonClient.getQueue("delayed_queue");

        // 创建一个延时队列
        RDelayedQueue<String> delayed = redissonClient.getDelayedQueue(delayedQueue);


        // 2. 创建发布者，发布延时消息到有序集合
        String message = "Hello, delayed message!";
        long delayInSeconds = 5;
        long timestamp = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(delayInSeconds);
        delayed.offer(message, delayInSeconds, TimeUnit.SECONDS);

        // 3. 创建订阅者，订阅有序集合中即将到期的消息，并在到期时执行相应的操作
//        delayed.addListener();

        // 注意：订阅者通常是异步的，所以这里暂停主线程，确保能接收到消息
        Thread.sleep(TimeUnit.SECONDS.toMillis(delayInSeconds + 1));

        redissonClient.shutdown();
    }

    // 自定义 ObjectListener 实现类
}

